package mytool;

import java.text.DecimalFormat;

/**
 * Created by zerdoor_pc on 2016/3/24.
 */
public class Distance {
    public static double getDisance(String lat1, String lng1, String lat2, String lng2) {

        //lat为纬度, lng为经度, 一定不要弄错
        double dis = 0;
        double radLat1 = toRad(lat1);
        double radLat2 = toRad(lat2);
        double deltaLat = radLat1 - radLat2;
        double deltaLng = toRad(lng1) - toRad(lng2);
        dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(deltaLng / 2), 2)));
        return dis * 6378137;

        //  alert(  getDisance(39.91917,116.3896,39.91726,116.3940) );

    }
    public static double getDisance(double lat1, double lng1, double lat2, double lng2) {

        //lat为纬度, lng为经度, 一定不要弄错
        double dis = 0;
        double radLat1 = toRad(lat1);
        double radLat2 = toRad(lat2);
        double deltaLat = radLat1 - radLat2;
        double deltaLng = toRad(lng1) - toRad(lng2);
        dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(deltaLng / 2), 2)));
        DecimalFormat df = new DecimalFormat("0.00");
        return Double.parseDouble(df.format(dis * 6378137));

        //  alert(  getDisance(39.91917,116.3896,39.91726,116.3940) );

    }

    private static double toRad(String d) {
        return Double.parseDouble(d) * Math.PI / 180;
    }private static double toRad(double d) {
        return  d * Math.PI / 180;
    }

}
